package com.jasic.comnt;

import com.jasic.common.GlobalConstant;
import com.jasic.common.ModuleConstant;
import com.jasic.common.exception.HandleException;
import com.jasic.comnt.cache.RequestCache;
import com.jasic.comnt.cache.ResponseCache;
import com.jasic.comnt.cache.SocketCache;
import com.jasic.comnt.cache.bean.RequestTask;
import com.jasic.comnt.cache.bean.ResponseResult;
import com.jasic.comnt.conn.PostSrvSocket;
import com.jasic.comnt.protocol.bean.PostionReq;
import com.jasic.comnt.protocol.bean.PostionResp;
import com.jasic.service.annotation.Module;
import com.jasic.service.module.AModuleable;
import com.jasic.service.module.IService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * User: Jasic
 * Date: 12-9-26
 */
@Module(name = ModuleConstant.NAME_INT_CONN, priority = ModuleConstant.PRIORITY_INT_CONN, status = ModuleConstant.STATUS_IN_CONN)
public class ConnServer extends AModuleable implements IService {


    private Logger logger = LoggerFactory.getLogger(this.getClass());

    //    private NioSocketConnector connector;

//    private void init() {
//
////        this.connector = new NioSocketConnector();
////        ConnectFuture future = connector.connect();
////        future.awaitUninterruptibly();
////        IoSession session = future.getSession();
////        session.write("OK,connect success!");
//        this.socket = SocketCache.getPostSocket();
//    }

//    @Override
    public void service() {

        new TestPutAndAddTask().start();

        while (true) {

            RequestTask<PostionReq> task = RequestCache.getPostTask();

            // 因为同步所以不需另起线程
            String id = task.getId();
            List<PostionResp> respList = new ArrayList<PostionResp>(task.getRequests().size());
            for (PostionReq req : task.getRequests()) {

                byte[] recv = null;
                try {
                    SocketCache.getPostSocket().send(req.toByte());
                    recv = SocketCache.getPostSocket().recv();
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error("Socket error。。。");
                }
                PostionResp resp = null;
                try {
                    resp = new PostionResp(recv);
                    resp.setPositionResult(GlobalConstant.POSTIONRESP_RESULT_SUCCESS);
                    resp.setTarget_id(req.getTarget_ID());
                    resp.setTarget_type(req.getTarget_type());
                } catch (HandleException e) {
                    resp = new PostionResp(GlobalConstant.POSTIONRESP_RESULT_UNSUCCESS, req.getTarget_ID(), req.getTarget_type(), 0, 0, 22.11111, 78.04, 0, 0, 0);
                    logger.error(e.getMessage());
                } finally {
                    respList.add(resp);
                }
            }
            ResponseResult<PostionResp> result = new ResponseResult<PostionResp>(id, respList);
            ResponseCache.putPostResp(result);
        }
    }

    class TestPutAndAddTask extends Thread{

        @Override
        public void run() {

            while(true){

                String id = UUID.randomUUID().toString();
                RequestTask<PostionReq> task = new RequestTask<PostionReq>();
                PostionReq req = new PostionReq("15989133047",1);

                task.setId(id);
                task.add(req);

                RequestCache.putPostTask(task);
                logger.info("放置了一个RequestTask---------");

                long t1 = System.currentTimeMillis();
                ResponseResult<PostionResp> result = ResponseCache.getPostResp(id);

                while(null == result || result.getRequests().size() == 0){

                    if(System.currentTimeMillis() - t1 > 10000){
                        System.out.println("没有拿到Response...");
                        break;
                    }

                    result = ResponseCache.getPostResp(id);
                }

                System.out.println(result);

            }
        }
    }
}
